<?php
defined('BY_JYA') or exit('error');
global $_RQ,$_AR;
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$return=array();
if($_RQ['op']=='list'){
	//搜索
	$params=array('uniacid'=>$_SESSION['uniacid'],'workerid'=>$this->worker_id);
	if($_RQ['title']){
		$params['title like']='%'.trim($_RQ['title']).'%';
		$return['params']['title']=$_RQ['title'];
	}
	$tip='这里显示我的助理：医生可添加多名助理协助医生处理问题(1)、可核销及管理核销助理 (2)、帮助医生发布文章(3)、发公告！';
	$pindex = max(1, intval($_RQ['page']));
	if($_RQ['num']){
		$psize = $_RQ['num'];
	}else{
		$psize = 15;
	}
	$total='';
	$parameter=array(
		'params'=>$params,
		'fields'=>$fields,
		'limit'=>array($pindex,$psize),
	);
	$list=pdo_getslice('jy_zhongyi_worker_zhuli',$params,array($pindex,$psize),$total,$fields,'','id desc');
	if($list){
		foreach($list as &$item){
			
		}
	}
	$return['list']=$list?$list:array();
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'tabs'=>array(
			'0'=>array('op'=>'list','title'=>'助理列表'),
			'1'=>array('op'=>'edit','title'=>'添加助理'),
		),
		'list'=>array('data'=>'list','tip'=>$tip,'list'=>array(
			array('name'=>'image','title'=>'助理头像','type'=>'image'),
			array('name'=>'title','title'=>'助理名称','type'=>'string'),
			array('type'=>'string','title'=>'用户名','name'=>'username'),
		)),
		'search_one'=>array('name'=>'title'),
	);
	$return['htmls']=$htmls;
	exi($return);
}

if($_RQ['op']=='edit'){
	$id=intval($_RQ['id']);
	if($id){
		$item=pdo_get('jy_zhongyi_worker_zhuli',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		unset($item['password']);
	}else{
		$item=array('id'=>0,'type'=>0);
	}
	$return['item']=$item;
	$htmls=array(
		'type'=>'edit',
		'post'=>'post',
		'tabs'=>array(
			array('op'=>'list','title'=>'助理列表'),
			array('op'=>'edit','title'=>'添加助理'),
		),
		'edit'=>array(
			array('type'=>'string','title'=>'助理名称','name'=>'title', 'help'=>''),
			array('name'=>'username','title'=>'用户名','type'=>'string'),
			array('name'=>'password','title'=>'密码','type'=>'password'),
			array('type'=>'text','title'=>'描述','name'=>'desc', 'help'=>''),
			array('type'=>'image','title'=>'头像','name'=>'image', 'help'=>''),
			array('type'=>'choose','title'=>'绑定会员','name'=>'mid','action'=>'core/member.get_member/','choose_title'=>'选择会员'),
			array('type'=>'switch','title'=>'是否显示','name'=>'status', 'help'=>''),
		)
	);
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='post'){
	$postdata=get_postdata();
	if(!$postdata['title']){
		exi('请填写助理名称！','error');
	}
	$data=array(
		'title'=>trim($postdata['title']),
		'desc'=>trim($postdata['desc']),
		'mid'=>trim($postdata['mid']),
		'image'=>trim($postdata['image']),
		'username'=>trim($postdata['username']),
		'status'=>trim($postdata['status'])
	);
	if($postdata['password']){
		$data['password']=base64_encode($postdata['password']);
	}
	if($data['username']){
		$uname=pdo_get('jy_zhongyi_worker_zhuli',array('username'=>$data['username'],'uniacid'=>$_SESSION['uniacid']));
	}
	$id=intval($postdata['id']);
	if($uname && $uname['id']!=$id){
		exi('账号不可以重复','error');	
	}
	if($id){
		pdo_update('jy_zhongyi_worker_zhuli',$data,array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		exi('助理修改成功！','','list');
	}else{
		if(!$postdata['username'] || !$postdata['password']){
			exi('请填写账户和密码！','error');
		}
		$data['workerid']=$this->worker_id;
		$data['uniacid']=$_SESSION['uniacid'];
		$data['createtime']=TIMESTAMP;
		pdo_insert('jy_zhongyi_worker_zhuli',$data);
		exi('助理增加成功！','','list');
	}
}
if($_RQ['op'] == 'del'){
	$id=intval($_RQ['id']);
	if($id){
		pdo_delete('jy_zhongyi_worker_zhuli',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}
	$del=trim($_RQ['del'],';');
	if($del){
		$del=explode(';',$del);
		foreach($del as $id){
			$id=intval($id);
			if($id){
				pdo_delete('jy_zhongyi_worker_zhuli',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
			}
		}
	}
	exi('删除成功！');
}

?>